<?php 
namespace Admin\Controller;
use Think\Controller;
use Think\Think;
use Common\Model\OptionModel;
class FuturesAgentController extends AdminController{
	public function index(){
		$sql = "";
		/*
		 * 搜索功能
		 * */
		//平台角色查询
		if($_REQUEST['pt_role']){
			if($_REQUEST['pt_role']==1){
				$sql['pt_role']=1;
				$status=1;
			}elseif($_REQUEST['pt_role']==2){
				$sql['pt_role']=2;
				$status=2;
			}else{
				$status=-1;
			}
		}else{
			$status=-1;
		}
		//用户查询
		if($_REQUEST['nickname']){
			$map['nickname']=array('like',"%".trim($_REQUEST['nickname'])."%");

			$nickname = M('follow')->where($map)->field('id')->select();

			foreach($nickname as $k=>$v){
				$routes[$k] = $v['id'];
			}
			$sql['user_id'] = array('in',$routes);
		}
		//代理商账户查询
		if($_REQUEST['user_name']){
			$sql['user_name']=array('like',"%".trim($_REQUEST['user_name'])."%");
		}
		//创建时间查询
		if ($_REQUEST['create_time']) {
			$time=strtotime($_REQUEST['create_time']);
			$start_time=mktime(00,00,00,date('m',$time),date('d',$time),date('Y',$time));
			$end_time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
			$sql['create_time'] = array('between',array($start_time,$end_time));
		}
		//分页
		$count = M('Futures_agent')->where($sql)->count();
		$Page       = new \Think\Page($count,10);
		$show       = $Page->show();// 分页显示输出
		//查询代理商信息
		$list = M('Futures_agent')->where($sql)
		->table('wp_futures_agent as a')
		->join('wp_follow as b on a.user_id = b.id')
		->field('b.id,b.nickname,b.mobile,a.user_id,a.user_name,a.create_time,a.market,a.sure_money,a.pt_role')
		->limit($Page->firstRow.','.$Page->listRows)
		->select();
		//查询下级，获取下级余额
		foreach($list as $k => $v){
			if($v['id'] == 1){
				$where[$k] = array('maker_id'=>$v['user_id']);
			}else{
				$where[$k] = array('_string' => "(route like '{$v['user_id']}') or (route regexp ',{$v['user_id']},') or (route regexp '^{$v['user_id']},') or (route regexp ',{$v['user_id']}$')");
			}
			$route[$k] = M('follow')->where($where[$k])->field('id,money')->select();
			foreach($route[$k] as $vo){//计算总和
				$sum[$k] = $vo['money']+$sum[$k];
				$sum[$k] = round($sum[$k],2);
			}
			$sum_money = $sum[$k]/4;
			if(!empty($route[$k])){
				if($sum_money > $v['sure_money']){
					$list[$k]['status'] = "保证金不足";
					$list[$k]['route_money'] = $sum[$k];
				}else{
					$list[$k]['status'] = "保证金充足";
					$list[$k]['route_money'] = $sum[$k];
				}
			}else{
				$list[$k]['status'] = "保证金充足";
				$list[$k]['route_money'] = 0;
			}
		}
		$this->assign('status',$status);
		$this->assign('_page',$show);
		$this->assign('list',$list);
		$this->display();
	}

	public function all_money(){
		$s_type = C('MAKER_MONEY_LOG_TYPE');
		$s_types = array_flip($s_type);
		$this->assign('s_types',$s_types);
		$id=I('get.id');
		$where['maker_id']=intval($id);
		$start_time=I('get.start_time');
		if($start_time){
			$start_time=strtotime($start_time);
			$where['create_time']=array('gt',$start_time);
			$this->assign('start_time',$start_time);
		}
		$end_time=I('get.end_time');
		if($end_time){
			$end_time=strtotime($end_time)+86400;
			$where['create_time']=array('lt',$end_time);
			$this->assign('end_time',$end_time);
		}
		if($start_time && $end_time){
			$where['create_time']=array('$gte'=>$start_time,'$lte'=>$end_time);
		}
		$status=I('get.status');
		if($status!=-2){
			$where['money_type']=intval($status);
			$this->assign('status',$status);
		}
		$user_name=I('get.user_name');
		if($user_name){
			$wherename['nickname']=array('like','%'.$user_name.'%');
			$namedata=M('follow')->where($wherename)->select();
			$idarr=array();
			foreach ($namedata as $v) {
				$idarr[] = $v['id'];
			}
			$where['user_id']=array('in',$idarr);
		}
		$username=M('follow')->where('id='.$where['maker_id'])->find();
		ini_set('mongo.long_as_object', 1);
		$Model=new OptionModel('maker_log');
		$size=$Model->where($where)->count();
		$page=new \Think\Page($size,50);
        $data=$Model->where($where)->limit($page->firstRow.','.$page->listRows)->select();
		$arr=array();
		foreach($data as $key=>$val){
			$u=$val['user_id'];
			$t=$val['money_type'];
			$user=M('follow')->where('id='.$u)->find();
			$arr[$u][$t]+=$val['money'];
			$huidata[$u][$t]=$val;
			$huidata[$u][$t]['money']=$arr[$u][$t];
			$huidata[$u][$t]['user_id']=$user['nickname'];
		}
		$showdata=$Model->where($where)->select();
		$topmoney="";
		foreach($showdata as $vk){
			$topmoney+=$vk['money'];
		}
		$this->assign('topmoney',$topmoney);
		$this->assign('huidata',$huidata);
		$this->assign('page',$page);
		$this->assign('username',$username);
		$this->assign('id',$id);
		$this->display();
	}

	public function agent_all_money(){
		$s_type = C('MONEY_LOG_TYPE');
		$s_types = array_flip($s_type);
		$this->assign('s_types',$s_types);
		$id=I('get.id');
		//搜索功能
		$status=I('get.status');
		if($status!=-2){
			$sql['type']=$status;
			$this->assign('status',$status);
		}
		$start_time=I('get.start_time');
		if($start_time){
			$start_time=strtotime($start_time);
			$sql['create_time']=array('egt',$start_time);
			$this->assign('start_time',$start_time);
		}
		$end_time=I('get.end_time');
		if($end_time){
			$end_time=strtotime($end_time)+86400;
			$sql['_string']="create_time <=".$end_time;
			$this->assign('end_time',$end_time);
		}
		//先查询该代理商的所有下级
		$where['_string']="(route like '{$id}') or (route regexp ',{$id},') or (route regexp '^{$id},') or (route regexp ',{$id}$')";
		$data=M('follow')->where($where)->select();

		foreach($data as $val){
			$arr[]=$val['id'];
		}
		$sql['user_id']=array('in',$arr);
		$user_name=I('get.nickname');
		if($user_name){
			$search['nickname']=array('like','%'.$user_name.'%');
			$userdata=M('follow')->where($search)->select();
			foreach($userdata as $value){
				$idarr[]=$value['id'];
			}
			$sql['user_id']=array('in',$idarr);
			$this->assign('user_name',$user_name);
		}
		$querysql=M('futures_fund_record')->field('*,sum(money) as topmoney')->where($sql)->group('user_id,type')->select();
		foreach($querysql as $key=>$v){
			$username=M('follow')->where('id='.$v['user_id'])->find();
			$querysql[$key]['nickname']=$username['nickname'];
		}
		$showdata=M('futures_fund_record')->field('*,sum(money) as showmoney')->where($sql)->select();
		$this->assign('showdata',$showdata);
		$this->assign('querysql',$querysql);
		$this->assign('id',$id);
		$this->display();
	}
}
?>